What is deglob?
The deglob npm package is used to expand glob patterns into an array of file paths, with options to ignore certain patterns and to use specific file extensions. It is particularly useful for tools that need to process files based on patterns, such as linters or build systems.
What are deglob's main functionalities?
Basic Usage
This feature allows you to expand a glob pattern into an array of file paths. In this example, it will find all JavaScript files in the current directory and its subdirectories.
const deglob = require('deglob');
deglob(['**/*.js'], (err, files) => {
if (err) {
console.error(err);
return;
}
console.log(files);
});
Ignoring Patterns
This feature allows you to specify patterns to ignore. In this example, it will find all JavaScript files but ignore those in the 'node_modules' and 'dist' directories.
const deglob = require('deglob');
deglob(['**/*.js'], { ignore: ['node_modules/**', 'dist/**'] }, (err, files) => {
if (err) {
console.error(err);
return;
}
console.log(files);
});
Using Specific File Extensions
This feature allows you to specify multiple file extensions in the glob pattern. In this example, it will find all JavaScript and JSX files.
const deglob = require('deglob');
deglob(['**/*.{js,jsx}'], (err, files) => {
if (err) {
console.error(err);
return;
}
console.log(files);
});
Other packages similar to deglob
glob
The 'glob' package is a low-level library for matching files using glob patterns. It is more flexible but requires more configuration compared to deglob.
fast-glob
The 'fast-glob' package is a high-performance alternative to 'glob'. It is faster and more efficient, especially for large sets of files, but does not include built-in support for ignoring patterns like deglob.
globby
The 'globby' package is a higher-level wrapper around 'glob' and 'fast-glob' that provides a simpler API and additional features like ignoring patterns and multiple file extensions, similar to deglob.
deglob
Take a list of glob patterns and return an array of file locations, respecting .gitignore
and allowing for ignore patterns via package.json
.
Giant swaths of this code were extracted from standard. It seems useful outside of that tool, so I've attempted to extract it! :)
Install
npm install --save deglob
Usage
var deglob = require('deglob')
deglob(['**/*.js'], function(err, files) {
files.forEach(function(file) {
console.log('found file ' + file)
})
})
var path = require('path')
var opts = {
cwd: path.join(__dirname, 'someDir'),
useGitIgnore: false,
usePackageJson: false
}
deglob(['**/*.js'], opts, function(err, files) {
files.forEach(function(file) {
console.log('found file ' + file)
})
})
Ignoring files in package.json
deglob
will look for a package.json
file by default and use any ignore patterns defined.
To define patterns in package.json add somthing like this:
"config": {
"ignore": ['**/*.bad']
}
If you do not fancy the config
key, provide a different one using the configKey
option.
Options
Option | Default | Description |
---|
useGitIgnore | true | Turn on/off allowing ignore patterns via .gitignore |
usePackageJson | true | Turn on/off allowing ignore patterns via package.json config. |
configKey | 'config' | This is the parent key in package.json to look for the ignore attribute. |
gitIgnoreFile | '.gitignore' | Name of the .gitignore file look for (probably best to leave it default) |
ignore | [] | List of additional ignore patterns to use |
cwd | process.cwd() | This is the working directory to start the deglobbing |
Contributing
Contributions welcome! Please read the contributing guidelines first.
License
ISC